﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class AdaugareEvaluareSisteme : UserControl
    {
        private Control redControl;
        string CNP;
        string limba;

        public AdaugareEvaluareSisteme(string limba,string CNP)
        {
            InitializeComponent();
            Rename(limba);

            this.CNP = CNP;
            this.limba = limba;
        }
             
        private void btAdaugaEvaluareSistem_Click(object sender, EventArgs e)
        {
            if (redControl != null)
                changeColor(redControl);

            EvaluareSisteme es = new EvaluareSisteme();
            es.IdPacient = CNP;
            es.DataEvaluari = dtpDataEvaluarii.Value;
            es.TegumenteMucoase = tbTegumenteMucoase.Text;
            es.SistemCelularSubcutanat = tbSistemCelularSubuctanat.Text;
            es.SistemGanglionar = tbSistemGanglionar.Text;
            es.SistemOsteoarticular  = tbSistemOsteoarticular.Text;
            es.SistemCardiovascular = tbSistemCardioVascular.Text;
            es.SistemRespirator = tbSistemRespirator.Text;
            es.SistemDigestiv  = tbSistemDigestiv.Text;
            es.SistemGenitoUrinar = tbSistemGenitoUrinar.Text;
            es.Snc = tbSNC.Text;
            es.ExaminareParacilinica = tbExaminareParaclinica.Text;
            es.Recomandari = tbRecomandari.Text;
            es.Concluzii = tbConcluzii.Text;

            List<Control> toValidate = new List<Control>();
            bool isValid = true;

            // Validare EvaluareSisteme;
            toValidate.Add(dtpDataEvaluarii);
            toValidate.Add(tbTegumenteMucoase);
            toValidate.Add(tbSistemCelularSubuctanat);
            toValidate.Add(tbSistemGanglionar);
            toValidate.Add(tbSistemOsteoarticular);
            toValidate.Add(tbSistemCardioVascular);
            toValidate.Add(tbSistemRespirator);
            toValidate.Add(tbSistemDigestiv);
            toValidate.Add(tbSistemGenitoUrinar);
            toValidate.Add(tbSNC);
            toValidate.Add(tbExaminareParaclinica);
            toValidate.Add(tbRecomandari);
            toValidate.Add(tbConcluzii);

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");


            foreach (Control tb in toValidate)
            {
                Validation val = new Validation(tb, tb.Text);
                isValid = val.validate();

                if (isValid == false)
                {
                    string eroare = val.getError();
                    redControl = tb;
                    MessageBox.Show(errors["CampIncorect"], "Error", 
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }

            //salvare in BD           
            if (isValid == true)
            {
                try
                {
                    PersistenceManager pm = PersistenceManager.Instance;
                    pm.Save(es);
                    MessageBox.Show(errors["okBD"], "Information",
                           MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show(errors["ErrorBD"], "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void changeColor(Control cont)
        {
            if (cont.GetType() == typeof(TextBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(DateTimePicker))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(ComboBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
        }
    }
}
